﻿package  {
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Graphics.*;
	import flash.events.Event;
	import flash.filters.*;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	
	
	public class Wheel extends MovieClip {
		
		public var ico_height:Number =  128;
		public var ico_width:Number =  128;
		
		public var ease1;
		public var ease2;
		public var ease3;
		
		
		public var icons_now:Array; //now showing
		
		public var inums:Array = new Array(); //current line numbers;
		public var icons:Array = new Array(); //current icons clips
		
		public var times:int = 5;
		public var counter:int = 5;
		public var speed:Number = 75;
		
		public var icons_holder:MovieClip; //clip for icons
		public var icons_mask:Shape; //mask for clips;
		
		
		
		public var onStartWheel:Function = function(){};
		public var onFastWheel:Function = function(){};
		public var onSlowWheel:Function = function(){};
		public var onStopWheel:Function = function(){};
		
		
		
		
		
		
		
		
		
		
		public function Wheel() {
			super();
			//Create animation easings. replace with motionBlur in future
			CustomEase.create("myCustomEase1",[{s:0,cp:-0.09,e:0.16},{s:0.16,cp:0.41,e:1}]);
			CustomEase.create("myCustomEase2", [{s:0,cp:0.102,e:0.352},{s:0.352,cp:0.602,e:1}]);
			CustomEase.create("myCustomEase3", [{s:0,cp:0.646,e:0.896},{s:0.896,cp:1.14599,e:1}]);
			this.ease1 = CustomEase.byName("myCustomEase1");
			this.ease2 = CustomEase.byName("myCustomEase2");
			this.ease3 = CustomEase.byName("myCustomEase3");
			
			
			//create first time icons
			this.icons_now = new Array(0,0,0);
			var i:int;
			for(i=0;i<15;i++)
			{
				this.inums.push(0);
			}
			//create icons holder;
			this.icons_holder = new MovieClip();
			
			//create weel mask
			this.icons_mask = new Shape();  
			this.icons_mask.graphics.beginFill(0x000000);  
			this.icons_mask.graphics.drawRect(0,0, this.ico_width,this.ico_height*3);
			this.icons_mask.graphics.endFill();  
			
			this.icons_holder.mask = this.icons_mask;
			this.addChild(this.icons_holder);
			this.addChild(this.icons_mask);
			//create icons
			for(i=0;i<15;i++)
			{
				this.icons.push(new WIcon());
				this.icons[i].x = 0;
				this.icons[i].y = ((i-11)*this.ico_height);
				this.icons_holder.addChild(this.icons[i]);
			}
			
			// add stage listener
			this.addEventListener(Event.ADDED_TO_STAGE, this.onStage);
		}
		
		
		
		public function onStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onStage);
		}
		
		public function resetWheel(ico1:int,ico2:int,ico3:int):void
		{
			this.icons_now = new Array(ico1,ico2,ico3);
			this.inums = new Array();
			for(var i=0;i<15;i++)
			{
				switch(i)
				{
					case 11:
						this.inums.push(ico1);
					break;
					case 12:
						this.inums.push(ico2);
					break;
					case 13:
						this.inums.push(ico3);
					break;
					default:
						this.inums.push(Helper.random(1,12));
					break;
				}
				this.icons[i].showIcon(this.inums[i]);
			}
			this.icons_holder.y = 0;
			this.icons_holder.cacheAsBitmap = false;
		}
		
		//start weel;
		
		public function startWheelTo(ico1:int,ico2:int,ico3:int, speed:Number = 75, counts:int = 6):void
		{
			this.icons_now = new Array(ico1,ico2,ico3); // icons set at last tween
			TweenMax.killChildTweensOf(this.icons_holder); // if now tweening;
		
			this.times = counts;
			this.speed = speed;
			
			this.counter = counts+1;
			
			var myBlur:BlurFilter = new BlurFilter(0,2,2);
			this.icons_holder.filters = [myBlur];
			
			TweenMax.to(this.icons_holder, (1-this.speed/100) * 4.4, {y:(10*this.ico_height), ease:this.ease1 ,onComplete:this.switchStep });
		}
		
		
		
		
		
		
		
		public function stopWheel():void
		{
			this.counter = 0;
		}
		
		public function switchStep():void
		{
			this.counter --;
			
			var nowvis:Array = this.inums.slice(0,5);
			this.inums = nowvis;
			var i:int;
			for(i=0;i<10;i++)
			{
				this.inums.unshift(Helper.random(1,12));
			}
			for(i=0;i<15;i++)
			{
				this.icons[i].showIcon(this.inums[i]);
			}
			this.icons_holder.y = 0;
			if(this.counter <= 0)
			{
				this.finishMove();
			} else {
				this.regularMove();
			}
		}
		
		public function regularMove():void
		{
			var myBlur:BlurFilter = new BlurFilter(0,16,2);
			this.icons_holder.filters = [myBlur];
			TweenMax.to(this.icons_holder, (1-this.speed/100) * 2.4, {y:(10*this.ico_height), ease:Linear.easeNone ,onComplete:this.switchStep });
		}
		
		public function finishMove():void
		{
			this.inums[1] = this.icons_now[0];
			this.inums[2] = this.icons_now[1];
			this.inums[3] = this.icons_now[2];
			this.icons[1].showIcon(this.inums[1]);
			this.icons[2].showIcon(this.inums[2]);
			this.icons[3].showIcon(this.inums[3]);
			
			var myBlur:BlurFilter = new BlurFilter(0,2,2);
			this.icons_holder.filters = [myBlur];
			TweenMax.to(this.icons_holder, (1-this.speed/100) * 4.4, {y:(10*this.ico_height), ease:this.ease3,onComplete:this.moveDone});
		}
		
		public function moveDone():void
		{
			var myBlur:BlurFilter = new BlurFilter(0,0,0);
			this.icons_holder.filters = [myBlur];
			this.resetWheel(this.icons_now[0],this.icons_now[1],this.icons_now[2]);
			
			this.onStopWheel();
		}

	}
	
}
